//
//  FindPasswordViewController.m
//  CommunityNannyO2O
//
//  Created by 90 on 15/7/21.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "FindPasswordViewController.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "UIViewController+Create.h"
#import "CustomTextField.h"
#import "SetPasswordViewController.h"
#import "CommFunc.h"
#import "VerificationModel.h"

@interface FindPasswordViewController()<QXNetWorkToolDelegate>
{
    NSInteger timeCounter;
    NSTimer *timer;
}

@property (weak, nonatomic) IBOutlet UILabel  *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel  *phoneVerificationLabel;
@property (weak, nonatomic) IBOutlet UILabel  *verificationLabel;

@property (strong, nonatomic) IBOutlet CustomTextField  *phoneText;
@property (strong, nonatomic) IBOutlet CustomTextField  *verificationCodeText;
@property (weak, nonatomic) IBOutlet UIButton *sendVerficationCode;
@property (weak, nonatomic) IBOutlet UIButton *verfication;
@property (strong, nonatomic) VerificationModel *code;


-(void)setLabelColor:(UIColor*)color Index:(int)index;

@end


@implementation FindPasswordViewController

-(void)setLabelColor:(UIColor*)color Index:(int)index
{
    UIColor *temp = UIColorRGBA(0, 0, 0, 1);
    self.phoneLabel.textColor = temp;
    self.phoneVerificationLabel.textColor = temp;
    self.verificationLabel.textColor = temp;
    
    if (index == 0)
    {
        self.phoneLabel.textColor = color;
    }
    else if (index == 1)
    {
        self.phoneVerificationLabel.textColor = color;
    }
    else if (index == 2)
    {
        self.verificationLabel.textColor = color;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //叉号，一次性删除输入框中的内容
    [self.phoneText setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.verificationCodeText setClearButtonMode:UITextFieldViewModeWhileEditing];
    
    //数字键盘
    self.phoneText.keyboardType = UIKeyboardTypeNumberPad;
    self.verificationCodeText.keyboardType = UIKeyboardTypeNumberPad;
    
    //编辑时清空
    self.phoneText.clearsOnBeginEditing = YES;
    self.verificationCodeText.clearsOnBeginEditing = YES;
    
    self.verfication.layer.cornerRadius = 5;
    self.verfication.layer.masksToBounds = YES;
    [self.verfication.layer setBorderWidth:1.0]; //边框宽度
    [self.verfication.layer setBorderColor:UIColorRGBA(255, 255, 255, 1).CGColor];//边框颜色
    
    //placeholder字体、颜色
    [self.phoneText setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:15]
                  Color:UIColorRGBA(174, 174, 174, 1)];
    
    [self.verificationCodeText setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:13] Color:UIColorRGBA(174, 174, 174, 1)];
    
    [self.verfication addTarget:self action:@selector(verfication:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.sendVerficationCode addTarget:self action:@selector(verficationCodeSender:) forControlEvents:UIControlEventTouchUpInside];
    
    //手势
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
    //子view
    UIView *containView1 = [self.view viewWithTag: 1];
    
    UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView1 addGestureRecognizer:tapGestureRecognizer1];
    
    //子view
    UIView *containView2 = [self.view viewWithTag: 2];
    
    UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView2 addGestureRecognizer:tapGestureRecognizer2];

}

-(void)tap
{
    [self.phoneText resignFirstResponder];
    [self.verificationCodeText resignFirstResponder];
}

- (BOOL)isCorrectPhone
{
    if ([self.phoneText.text isEqualToString:@""])
    {
        [MBProgressHUD showError:@"请输入手机号码" toView:self.view];
        return NO;
    }

    if ([CommFunc isPureInt:self.phoneText.text])
    {
        if(![CommFunc isValidateMobile:self.phoneText.text])
        {
            [MBProgressHUD showError:@"请正确输入手机号" toView:self.view];
            
            return NO;
        }
    }
    else
    {
        [MBProgressHUD showError:@"请正确输入手机号" toView:self.view];
        
        return NO;
    }
    
    return YES;
}


-(void)timeFireMethod
{
    if (timeCounter < 0)
    {
        return;
    }
    
    timeCounter--;
    
    [self.sendVerficationCode setTitleColor:UIColorRGBA(230, 69, 68, 1) forState:UIControlStateNormal];
    
    self.sendVerficationCode.titleLabel.font =
    [UIFont fontWithName:DefaultFont size:12];
    
    [self.sendVerficationCode setTitle:[NSString stringWithFormat:@"重新发送(%ld)",(long)timeCounter] forState:UIControlStateNormal];
    
    self.sendVerficationCode.userInteractionEnabled = NO;
    
    if(timeCounter <= 0)
    {
        [timer invalidate];
        timer = nil;
        [self.sendVerficationCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.sendVerficationCode.userInteractionEnabled = YES;
    }
}

//验证
- (void)verfication:(id)sender
{
    if ([self.verificationCodeText.text isEqualToString:@""])
    {
        [MBProgressHUD showError:@"请输入验证码" toView:self.view];
        return;
    }
    
    if (![self.verificationCodeText.text isEqualToString:self.code.validate_num])
    {
        [MBProgressHUD showError:@"验证码错误" toView:self.view];
        return;
    }
    
    [self setLabelColor:MIDIDefaultColor Index:2];
    
    SetPasswordViewController *obj = [SetPasswordViewController create];
    obj.phone = self.phoneText.text;
    
    [self.navigationController pushViewController:obj animated:YES];
}

//发送验证码
- (void)verficationCodeSender:(id)sender
{
    if ([self isCorrectPhone] == NO)
    {
        return;
    }

    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:VerificationCodeInterface andParameter:@[@{@"phone":self.phoneText.text}] tag:1];
}

-(void)configNavigationItem
{
    self.navigationItem.title = @"找回密码";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_navigation_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //设置导航条title
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
    shadow.shadowOffset = CGSizeMake(0, 0);
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                [UIColor whiteColor],NSForegroundColorAttributeName,
                            shadow,NSShadowAttributeName,
        [UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,
                                                                     nil]];
    
    //status bar的字体为白色
    
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self configNavigationItem];

    [self useMethodToFindBlackLineAndHind];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showError:message];
            break;
    }
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    switch (tag)
    {
        case 1:
        {
            [MBProgressHUD showSuccess:@"获取验证码成功"];
            
            for (NSDictionary *dic in Response)
            {
                self.code = [VerificationModel VerificationWithDict:dic];
                break;
            }
            
            timeCounter = 61;//60秒倒计时
            
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            
            [self setLabelColor:MIDIDefaultColor Index:1];
            break;
        }
    }
}


@end




















